package com.qianbao.service.money.impl;

import com.qianbao.components.excepion.CommonException;
import com.qianbao.components.excepion.ExceptionEnum;
import com.qianbao.dao.money.CoinDao;
import com.qianbao.dao.money.ReadTimeDao;
import com.qianbao.domain.money.CoinNumberTipsEntity;
import com.qianbao.service.coinrecord.CoinRecordService;
import com.qianbao.service.money.CoinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class CoinServiceImpl implements CoinService {

    @Autowired
    private CoinDao coinDao;


    @Autowired
    private ReadTimeDao readTimeDao;


    @Autowired
    private CoinRecordService recordService;


    @Override
    public void saveUserCoin(String customId, String coin) throws CommonException {

        if (customId == null || customId.length() == 0 || coin == null || coin.length() == 0){
            throw new CommonException(ExceptionEnum.Request_Params_Invalid.getCode(),ExceptionEnum.Request_Params_Invalid.getMessage());
        }

        int rows = coinDao.saveUserCoin(customId, Integer.parseInt(coin));

        if (rows <= 0){
            throw new CommonException(ExceptionEnum.Sql_update_fail.getCode(),ExceptionEnum.Sql_update_fail.getMessage());
        }

    }


    @Override
    public int findUserCoin(String custom_id) throws CommonException {

        if (custom_id == null || custom_id.length() == 0){
            throw new CommonException(ExceptionEnum.Request_Params_Invalid.getCode(),ExceptionEnum.Request_Params_Invalid.getMessage());
        }

        return coinDao.findUserCoin(custom_id);
    }


    @Transactional
    @Override
    public Map exchangeReadtimeToCoin(String customId) throws CommonException {

        // 查询用户的阅读时长
        int readTime = readTimeDao.findUserReadTime(customId);

        // 把用户的阅读时长清零
        readTimeDao.updateUserReadTime(customId,0);

        // 更新用户的金币  这个50要能配置就好了
        int coin = readTime * 50;
        coinDao.saveUserCoin(customId, coin);

        // 返回用户当前的金币数量 阅读时长
        int currentCoin = coinDao.findUserCoin(customId);
        int currentTime = readTimeDao.findUserReadTime(customId);

        HashMap<String,String> map = new HashMap<>();
        map.put("coin",String.valueOf(currentCoin));
        map.put("readtime",String.valueOf(currentTime));

        return map;

    }

    @Transactional
    @Override
    public void saveUserCoinNumberTips(CoinNumberTipsEntity entity) throws CommonException {

        int rows = coinDao.saveUserCoin(entity.getCustom_id(), Integer.parseInt(entity.getCoinNumber()));

        if (rows <= 0){
            throw new CommonException(ExceptionEnum.Sql_update_fail.getCode(),ExceptionEnum.Sql_update_fail.getMessage());
        }

        recordService.saveCoinRecord(entity.getCustom_id(),entity.getCoinNumber(),entity.getTips());

    }







}





















